'use strict';

const Service = require('egg').Service;

class DatahandleService extends Service {
    async insertdata(hid) {
        let pinlunfromusersql = `SELECT eavcontext,factualaccounts,convenientlocation,clean,higher  FROM pinlun WHERE hid=${hid}`;
        let resarr = await this.app.mysql.query(pinlunfromusersql);
        console.log(resarr);
        let jishu = resarr.length;
        let pinluncount = 0;
        let totalpoint = 0;
        let rsmspoint = 0;
        let wzblpoint = 0;
        let gjwspoint = 0;
        let gxjbpoint = 0;
        resarr.forEach((el) => {
            rsmspoint += el.factualaccounts;
            wzblpoint += el.convenientlocation;
            gjwspoint += el.clean;
            gxjbpoint += el.higher;
            if (!el.eavcontext == '') {
                pinluncount++;
            }
        });
        totalpoint = (rsmspoint + wzblpoint + gjwspoint + gxjbpoint) / jishu / 4;
        rsmspoint = rsmspoint / jishu;
        wzblpoint = wzblpoint / jishu;
        gjwspoint = gjwspoint / jishu
        gxjbpoint = gxjbpoint / jishu;
        let pinisexitsql = `SELECT *  FROM house_evaluate WHERE hid=${hid}`;
        let zhixingsql = await this.app.mysql.query(pinisexitsql);
        if (!zhixingsql[0]) {
            let housepinlunsql = `INSERT INTO house_evaluate (hid,rsmspoint,wzblpoint,gjwspoint,gxjbpoint,totalpoint,pinluncount) VALUES (${hid},${rsmspoint},${wzblpoint},${gjwspoint},${gxjbpoint},${totalpoint},${pinluncount})`;
            this.app.mysql.query(housepinlunsql);
        } else if(zhixingsql[0]){
            let updatepinlunsql=`UPDATE house_evaluate SET rsmspoint=${rsmspoint},wzblpoint=${wzblpoint},gjwspoint=${gjwspoint},gxjbpoint=${gxjbpoint},totalpoint=${totalpoint},pinluncount=${pinluncount} WHERE hid=${hid}`;
            this.app.mysql.query(updatepinlunsql);
        }
        return;
    }
}

module.exports = DatahandleService;